<?php

namespace App\Http\Controllers;

use Auth;
use App\Repositories\WechatLoginRepository;
use Illuminate\Http\Request;

class WechatController extends Controller
{
    protected $wechatLoginRepository;

    public function __construct(WechatLoginRepository $wechatLoginRepository)
    {
        $this->wechatLoginRepository = $wechatLoginRepository;
    }

    public function base_login(Request $request)
    {
        $user    = session('wechat.oauth_user');
        $unionid = $user->original['unionid'];

        $user = $this->wechatLoginRepository->attemptBaseLogin($unionid);

        if ($user) {
            Auth::login($user);
            return redirect()->route('wap.home');
        } else {
            return redirect()->route('wap.wechat.login.userinfo');
        }
    }

    public function userinfo_login()
    {
        $user = session('wechat.oauth_user');
        $user = $this->wechatLoginRepository->attemptUserinfoLogin($user);

        if ($user) {
            Auth::login($user);
            return redirect()->route('wap.home');
        } else {
            return abort('500', '服务器忙，请稍后再试');
        }
    }
}
